/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.isip.transformation.difference;

import com.isip.transformation.Transformation;

/**
 *
 * @author Fauzan
 */
public class DifferenceFunction implements Transformation{

    @Override
    public int[][] doTransform(int[][] matrix) {
        int[][] new_matrix = new int[matrix.length][matrix[0].length];
        
        for(int i =0; i < matrix.length; i++ ) {
            for(int j=0; j < matrix[i].length; j++) {
                int n = matrix[i][j];
                int max = 0;
                int[] matrix9 = new int[8];
                int idx = 0;
                
                // Up left 0
                if(i > 0 && j > 0) {
                    matrix9[idx] = matrix[i-1][j-1];
                }
                
                idx++;
                // Center up 1        
                if(i > 0) {
                     matrix9[idx] = matrix[i-1][j];
                }
                
                idx++;
                // Up right 2
                if(i > 0 && j < matrix[i].length-1) {
                     matrix9[idx] = matrix[i-1][j+1];
                }
                
                idx++;
                // Center Right 3
                 if(j < matrix[i].length-1) {
                     matrix9[idx] = matrix[i][j+1];
                }
                 
                idx++;
                // Down Right 4
                if(i < matrix.length-1 && j < matrix[i].length-1) {
                       matrix9[idx] = matrix[i+1][j + 1];
                }
                
                idx++;
                // Center Down 5
                if(i < matrix.length-1) {
                    matrix9[idx] = matrix[i+1][j];
                }
                
                idx++;
                // Down Level 6
                if(i < matrix.length-1 && j > 0) {
                        matrix9[idx] = matrix[i+1][j-1];
                }
                
                idx++;
                // Center Left 7
                if(j > 0) {
                    matrix9[idx] = matrix[i][j-1];
                }
                
                int temp = Math.abs(matrix9[0] - matrix9[4]);
                if(max < temp) max = temp;
                
                temp = Math.abs(matrix9[1] - matrix9[5]);
                if(max < temp) max = temp;
                
                temp = Math.abs(matrix9[2] - matrix9[6]);
                if(max < temp) max = temp;
                
                temp = Math.abs(matrix9[3] - matrix9[7]);
                if(max < temp) max = temp;
                
                new_matrix[i][j] = max;
                
            }
        }
        
        return new_matrix;
    }
    
}
